/*
 *所有关于cc_task类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*cc_taskService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.MyTable.IRepositories;
using VOL.Core.ManageUser;
using System.Collections.Generic;
using System;
using VOL.Core.DBManager;
using MySql.Data.MySqlClient;
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using System.Net;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace VOL.MyTable.Services
{
    public partial class cc_taskService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Icc_taskRepository _repository;//访问数据库
        private WebResponseContent webResponse = new WebResponseContent();

        [ActivatorUtilitiesConstructor]
        public cc_taskService(
            Icc_taskRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override PageGridData<cc_task> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (IQueryable<cc_task> queryable) =>
            {
                if (options.Value == null)//从大屏过来的全部数据
                    return queryable; 
                else
                {//从系统中访问
                    var user = UserContext.Current.GetUserInfo(UserContext.Current.UserId);
                    if (user.UserTrueName != null) queryable = queryable.Where(x => x.zeren.Contains(user.UserTrueName) || user.UserTrueName == x.Creator||user.Role_Id==1);//数据隔离
                    return queryable;
                } 
            }; 
            return base.GetPageData(options);
        }
        
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            WebResponseContent webResponse = new WebResponseContent();
            AddOnExecuting = (cc_task task, object list) =>
            {
                var dd = DBServerProvider.DbContext.Set<cc_task>().Select(s => s.xuh ?? 0).Max();
                int num = dd + 1;
                task.xuh = num;
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecuting = (cc_task task, object addList, object updateList, List<object> delKeys) =>
            {
                if (task.progress == 100 && string.IsNullOrEmpty(task.sj_datetime.ToString()))
                {
                    return webResponse.Error("实际完成时间不能为空");
                }
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }
    }
}
